package cn.learn.validate.image;

import cn.learn.properties.ImageCodeProperties;
import cn.learn.properties.SecurityProperties;
import cn.learn.validate.ValidateCode;
import cn.learn.validate.ValidateCodeGenerator;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.util.Properties;

/**
 * 图形化验证码生成器
 *
 * @author huangyezhan
 * @date 2020年02月20日1:38
 */
@Setter
@Getter
public class ImageValidateCodeGenerator implements ValidateCodeGenerator {
    private static final Logger logger = LoggerFactory.getLogger(ImageValidateCodeGenerator.class);

    public void setSecurityProperties(SecurityProperties securityProperties) {
        properties = securityProperties.getCode().getImage();
    }

    private ImageCodeProperties properties;
    private Producer            producer;
    private DefaultKaptcha      defaultKaptcha;

    @Override
    public ValidateCode generate(HttpServletRequest request) {
        String width = request.getParameter("width");
        defaultKaptcha.getConfig().getProperties().setProperty("kaptcha.image.width", width);
        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image     = producer.createImage(text);
        ImageCode     imageCode = new ImageCode(image, text, properties.getExpireIn(), properties.getTotalValidations());
        return imageCode;
    }

    /**
     * 设置请求过来的图形验证码参数
     *
     * @return
     */
    private Properties setRequestKaptchaProperties() {

        return null;
    }

}
